<html>
<head>
    <script type="text/javascript">var oldAlert = window.alert, gMess; window.alert = function(message) {gMess = message; return true;};</script>
    <script type="text/javascript">window.alert = oldAlert;</script>
<script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
    var layer; 

    var name = 'Test Layer';
    var url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/export";
    var params = {layers: "show:0,2"};

    function test_Layer_AGS93_constructor (t) {
        var params = {layers: "show:0,2"};
        t.plan( 14 );

        var trans_format = "png";
        if (OpenLayers.Util.alphaHack()) { trans_format = "gif"; } 
        
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.ok( layer instanceof OpenLayers.Layer.ArcGIS93Rest, "new OpenLayers.Layer.ArcGIS93Rest returns object" );
        t.eq( layer.url, url, "layer.url is correct (HTTPRequest inited)" );
        t.eq( layer.params.LAYERS, "show:0,2", "params passed in correctly uppercased" );

        t.eq( layer.params.FORMAT, "png", "default params correclty uppercased and copied");

        t.eq(layer.isBaseLayer, true, "no transparency setting, wms is baselayer");

        params.format = 'jpg';
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq( layer.params.FORMAT, "jpg", "default params correclty uppercased and overridden");

        params.TRANSPARENT = "true";
        var layer2 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer2.isBaseLayer, false, "transparency == 'true', wms is not baselayer");

        params.TRANSPARENT = "TRUE";
        var layer3 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer3.isBaseLayer, false, "transparency == 'TRUE', wms is not baselayer");
        t.eq(layer3.params.FORMAT, trans_format, "transparent = TRUE causes non-image/jpeg format");

        params.TRANSPARENT = "TRuE";
        var layer4 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer4.isBaseLayer, false, "transparency == 'TRuE', wms is not baselayer");
        t.eq(layer4.params.FORMAT, trans_format, "transparent = TRuE causes non-image/jpeg format");

        params.TRANSPARENT = true;
        var layer5 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer5.isBaseLayer, false, "transparency == true, wms is not baselayer");
        t.eq(layer5.params.FORMAT, trans_format, "transparent = true causes non-image/jpeg format");

        params.TRANSPARENT = false;
        var layer6 = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.eq(layer6.isBaseLayer, true, "transparency == false, wms is baselayer");
    }
    
    function test_Layer_AGS93_addtile (t) {
        var params = {layers: "show:0,2"};
        t.plan( 6 );
    
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        var map = new OpenLayers.Map('map', {tileManager: null});
        map.addLayer(layer);
        var pixel = new OpenLayers.Pixel(5,6);
        var tile = layer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
        tile.draw();

        var img = tile.imgDiv;
        var tParams = OpenLayers.Util.extend({},
                        OpenLayers.Util.upperCaseObject(params));
        tParams = OpenLayers.Util.extend(tParams, {
            FORMAT: "png", BBOX: "1,2,3,4", SIZE: "256,256", F: "image", BBOXSR: "4326", IMAGESR: "4326"
        });
        t.eq( tile.url,
             url + "?" + OpenLayers.Util.getParameterString(tParams),
             "image src is created correctly via addtile" );
        t.eq( tile.getTile().style.top, "6px", "image top is set correctly via addtile" );
        t.eq( tile.getTile().style.left, "5px", "image top is set correctly via addtile" );

        var firstChild = layer.div.firstChild;
        t.eq( firstChild.nodeName.toLowerCase(), "img", "div first child is an image object" );
        t.ok( firstChild == img, "div first child is correct image object" );
        t.eq( tile.position.toString(), "x=5,y=6", "Position of tile is set correctly." );
        map.destroy();
    }
    
    function test_Layer_AGS93_inittiles (t) {
        var params = {layers: "show:0,2"};
        t.plan( 2 );
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params, {buffer: 2});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0),5);
        t.eq( layer.grid.length, 8, "Grid rows is correct." );
        t.eq( layer.grid[0].length, 7, "Grid cols is correct." );
        map.destroy();
    }


    function test_Layer_AGS93_clone (t) {
        var params = {layers: "show:0,2"};
        t.plan(4);
        
        var options = {tileSize: new OpenLayers.Size(500,50)};
        var map = new OpenLayers.Map('map', options);
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        map.addLayer(layer);

        layer.grid = [ [6, 7], 
                       [8, 9]];

        var clone = layer.clone();

        t.ok( clone.grid != layer.grid, "clone does not copy grid");

        t.ok( clone.tileSize.equals(layer.tileSize), "tileSize correctly cloned");

        layer.tileSize.w += 40;

        t.eq( clone.tileSize.w, 500, "changing layer.tileSize does not change clone.tileSize -- a fresh copy was made, not just copied reference");

        t.eq( clone.alpha, layer.alpha, "alpha copied correctly");

        layer.grid = null;
        map.destroy();
    }

    function test_Layer_AGS93_isBaseLayer(t) {
        var params = {layers: "show:0,2"};
        t.plan(3);
        
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        t.ok( layer.isBaseLayer, "baselayer is true by default");

        var newParams = OpenLayers.Util.extend({}, params);
        newParams.transparent = "true";
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, newParams);
        t.ok( !layer.isBaseLayer, "baselayer is false when transparent is set to true");

        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params, {isBaseLayer: false});
        t.ok( !layer.isBaseLayer, "baselayer is false when option is set to false" );
    }

    function test_Layer_AGS93_mergeNewParams (t) {
        var params = {layers: "show:0,2"};
        t.plan( 4 );

        var map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        
        var newParams = { layers: 'sooper', 
                          chickpeas: 'png'};

        map.addLayer(layer);
        map.zoomToMaxExtent();

        layer.redraw = function() {
            t.ok(true, "layer is redrawn after new params merged");
        }

        layer.mergeNewParams(newParams);
        
        t.eq( layer.params.LAYERS, "sooper", "mergeNewParams() overwrites well");
        t.eq( layer.params.CHICKPEAS, "png", "mergeNewParams() adds well");
    
        newParams.CHICKPEAS = 151;

        t.eq( layer.params.CHICKPEAS, "png", "mergeNewParams() makes clean copy of hashtable");
        map.destroy();
    }

    function test_Layer_AGS93_getFullRequestString (t) {
        var params = {layers: "show:0,2"};
        t.plan( 1 );
        var map = new OpenLayers.Map('map');
        map.projection = "xx";
        tParams = { layers: 'show:0,2', 
                   format: 'png'};
        var tLayer = new OpenLayers.Layer.ArcGIS93Rest(name, url, tParams);
        map.addLayer(tLayer);
        str = tLayer.getFullRequestString();
        var tParams = {
            LAYERS: "show:0,2", FORMAT: "png"
        };
        t.eq(str,
             url + "?" + OpenLayers.Util.getParameterString(tParams),
             "getFullRequestString() adds SRS value");
        map.destroy();
 
    }

    function test_Layer_AGS93_noGutters (t) { 
        t.plan(2);
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.ArcGIS93Rest("no gutter layer", url, params, {gutter: 0});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0), 5);
        var tile = layer.grid[0][0];
        var request = layer.getURL(tile.bounds);
        var args = OpenLayers.Util.getParameters(request);
        t.eq(parseInt(args['SIZE'][0]),
             tile.size.w,
             "layer without gutter requests images that are as wide as the tile");
        t.eq(parseInt(args['SIZE'][1]),
             tile.size.h,
             "layer without gutter requests images that are as tall as the tile");
        
        layer.destroy();
        map.destroy();
    }

    function test_Layer_AGS93_gutters (t) { 
        var params = {layers: "show:0,2"};
        t.plan(2);
        var gutter = 15;
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.ArcGIS93Rest("gutter layer", url, params, {gutter: gutter});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0), 5);
        var tile = layer.grid[0][0];
        var request = layer.getURL(tile.bounds);
        var args = OpenLayers.Util.getParameters(request);
        t.eq(parseInt(args['SIZE'][0]),
             tile.size.w + (2 * gutter),
             "layer with gutter requests images that are wider by twice the gutter");
        t.eq(parseInt(args['SIZE'][1]),
             tile.size.h + (2 * gutter),
             "layer with gutter requests images that are taller by twice the gutter");

        layer.destroy();
        map.destroy();

    }

    function test_Layer_AGS93_destroy (t) {

        t.plan( 1 );

        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        map.addLayer(layer);

        map.setCenter(new OpenLayers.LonLat(0,0), 5);

        //grab a reference to one of the tiles
        var tile = layer.grid[0][0];        

        layer.destroy();
        
    // checks to make sure superclass (grid) destroy() was called    
        
        t.ok( layer.grid == null, "grid set to null");
    }
    
    function test_Layer_ADG93_Filter(t) {
        var params = {layers: "show:0,2"};
        t.plan( 9 );
    
        layer = new OpenLayers.Layer.ArcGIS93Rest(name, url, params);
        var map = new OpenLayers.Map('map', {tileManager: null});
        map.addLayer(layer);
        var pixel = new OpenLayers.Pixel(5,6);
        var tile = layer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
        // Set up basic params.
        var tParams = OpenLayers.Util.extend({}, OpenLayers.Util.upperCaseObject(params));
        tParams = OpenLayers.Util.extend(tParams, {
            FORMAT: "png", BBOX: "1,2,3,4", SIZE: "256,256", F: "image", BBOXSR: "4326", IMAGESR: "4326"
        });
        
        // We need to actually set the "correct" url on a dom element, because doing so encodes things not encoded by getParameterString.
        var encodingHack = document.createElement("img");
        
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src no filter" );
        
        layer.setLayerFilter('1', "MR_TOAD = 'FLYING'");
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'FLYING';";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src one filter" );

        layer.setLayerFilter('1', "MR_TOAD = 'NOT FLYING'");
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src change one filter" );

        layer.setLayerFilter('2', "true = false");
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';2:true = false;";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src two filters" );

        layer.setLayerFilter('99', "some_col > 5");
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';2:true = false;99:some_col > 5;";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src three filters" );
        
        layer.clearLayerFilter('2');
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';99:some_col > 5;";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src removed middle filter" );
        
        layer.clearLayerFilter('2');
        tParams["LAYERDEFS"] = "1:MR_TOAD = 'NOT FLYING';99:some_col > 5;";
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src removed missing filter (no change)" );
        
        layer.clearLayerFilter();
        delete tParams["LAYERDEFS"];
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src removed all filters" );
        
        layer.clearLayerFilter();
        tile.draw();
        t.eq( tile.url, url + "?" + OpenLayers.Util.getParameterString(tParams), "image src removed all (no) filters" );
    }
    
    

  </script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>
